import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

public class Demo
{  public static void main(String args[]) {

    new ThreadFrame();
    }
}

class ThreadFrame extends JFrame implements ActionListener
{
    JTextField  showWord;
    JButton button;
    JTextField inputText,scoreText;
    JLabel ID=new JLabel();
    WordThread  giveWord;//内部有一个giveWord副线程
    int score=0;
    ThreadFrame() { //设置显示文字区域的样式
        showWord = new JTextField(6) ;
        showWord.setFont(new Font("", Font.BOLD, 72));
        showWord.setHorizontalAlignment(JTextField.CENTER);

        //设置副线程
        giveWord = new WordThread();
        giveWord.setJTextField(showWord);
        giveWord.setSleepLength(5000);

        //设置其他组件
        button = new JButton("开始");
        inputText = new JTextField(10);
        scoreText = new JTextField(5);

        //设置其他组件监视器及位置
        button.addActionListener(this);
        inputText.addActionListener(this);
        add(button, BorderLayout.NORTH);
        add(showWord, BorderLayout.CENTER);
        scoreText.setEditable(false);

        //BorderLayout布局，最下方插入画板，内含三个组件
        Panel southP = new Panel();
        southP.add(new JLabel("输入标签所显示的字母后回车:"));
        southP.add(inputText);
        southP.add(scoreText);
        add(southP, BorderLayout.SOUTH);
        //设置窗口基本样式
        setBounds(100, 100, 350, 180);
        setVisible(true);
        validate();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
       public void actionPerformed(ActionEvent e) {
           //按钮监视器
           if (e.getSource() == button) {
               if (!(giveWord.isAlive())) {    //如果当前线程没开始创建一个线程
                   giveWord.setJTextField(showWord);
                   giveWord.setSleepLength(2000);
               }
               try {
                   giveWord.start();//giveWord调用方法start()
               } catch (Exception exe) {
               }
           }
           //输入框监视器
           else if (e.getSource() == inputText) {
               if (inputText.getText().equals(showWord.getText())) {
                   score++;//如果输入的字母和showWord中的内容相匹配，得一分
               }
           }
           scoreText.setText("得分:" + score);
           inputText.setText(null);
           }
}


class WordThread extends Thread {
    char word;
    char k = 'A';
    JTextField showWord;
    int sleepLength = 2000;

    public void setJTextField(JTextField t) {
        showWord = t;
        showWord.setEditable(false);//使此文本区域不能被编辑
    }

    public void setSleepLength(int n) {

        sleepLength = n;
    }

    public void run() {
        Random random=new Random();
        k = 'A';
        while (true) {
            word = (char) k;
            showWord.setText("" + word);
            try {
                Thread.sleep(2000);//调用sleep方法使得线程中断6000豪秒
            } catch (InterruptedException e) {
            }
            int number=random.nextInt(27);
            k='A';
            k+=number;
            if (k>='A'&&k<='Z') continue;
        }
    }
}

